package com.matt.docpro.controller;

import cn.hutool.core.date.DateUtil;
import com.matt.docpro.config.Utils;
import com.matt.docpro.model.Appointment;
import com.matt.docpro.model.Chat;
import com.matt.docpro.model.Message;
import com.matt.docpro.model.User;
import com.matt.docpro.service.LoginInfoService;
import com.matt.docpro.service.MessageService;
import com.matt.docpro.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * MessageController
 * Created by  on 2024/03/04.
 */
@Controller
public class MessageController {
    @Autowired
    LoginInfoService loginInfoService;
    @Autowired
    UserService userService;
    @Autowired
    MessageService messageService;

    @RequestMapping("/message")
    public String init(Model model, HttpServletRequest request, String targetId) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        model.addAttribute("user", user);
        List<User> users = userService.getOtherUsers(user.getId());
        User target = null;
        List<Message> messages = new ArrayList<>();
        List<Chat> chats = new ArrayList<>();
        if (targetId != null) {
            // 指定用户
            target = userService.getById(targetId);
            messages = messageService.getChatMessage(user.getId(), targetId);
        } else {
            // 第一个聊天
            target = userService.getById(users.get(0).getId());
            messages = messageService.getChatMessage(user.getId(), users.get(0).getId());
        }
        model.addAttribute("messages", messages);
        model.addAttribute("target", target);
        // 聊天列表
        for (User user1 : users) {
            Chat chat = new Chat();
            chat.setUserId(user1.getId());
            chat.setUserNm(user1.getName());
            chat.setUserIcon(user1.getIcon());

            Message latestMessage = messageService.getLatestMessage(user.getId(), user1.getId());
            if (latestMessage != null) {
                int unreadCount = messageService.getUnreadCount(user1.getId(), user.getId());
                chat.setTime(latestMessage.getTime());
                chat.setUnreadCount(unreadCount);
                chat.setLatestMsg(latestMessage.getContent());
            }
            chats.add(chat);
        }
        model.addAttribute("chats", chats);
        return "message";
    }

    @RequestMapping("/message/send")
    public String insertMsg(Model model, Message message, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        message.setId(System.currentTimeMillis()+"");
        message.setTime(DateUtil.date());
        message.setRead(false);
        messageService.insert(message);
        List<Message> messages = messageService.getChatMessage(message.getSenderId(), message.getReceiverId());
        model.addAttribute("messages", messages);
        model.addAttribute("user", user);
        User target = userService.getById(message.getReceiverId());
        model.addAttribute("target", target);
        return "message::content";
    }


    @RequestMapping("/message/get")
    public String getMsg(Model model, String targetId, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        model.addAttribute("user", user);
        // 当前聊天
        User target = userService.getById(targetId);
        List<Message> messages = messageService.getChatMessage(user.getId(), targetId);
        model.addAttribute("messages", messages);
        model.addAttribute("target", target);
        // 更新已读
        messageService.updateRead(targetId, user.getId());
        // 聊天列表
        List<User> users = userService.getOtherUsers(user.getId());
        List<Chat> chats = new ArrayList<>();
        for (User user1 : users) {
            Chat chat = new Chat();
            chat.setUserId(user1.getId());
            chat.setUserNm(user1.getName());
            chat.setUserIcon(user1.getIcon());

            Message latestMessage = messageService.getLatestMessage(user.getId(), user1.getId());
            if (latestMessage != null) {
                int unreadCount = messageService.getUnreadCount(user1.getId(), user.getId());
                chat.setTime(latestMessage.getTime());
                chat.setUnreadCount(unreadCount);
                chat.setLatestMsg(latestMessage.getContent());
            }
            chats.add(chat);
        }
        model.addAttribute("chats", chats);
        return "message::chat";
    }
}
